#!/usr/bin/env python
# encoding: utf-8
"""
@author:zwb
@file: musics.py
@contact: zwb15083976291@163.com
@time: 2021/12/03
"""

from django.core.exceptions import ValidationError
from django.core.validators import URLValidator
from rest_framework import serializers

from common.models import BlogMusics
from utils.func import whether_url
from utils.serializers.serializer import SpecifySerializeFieldModelSerializer, UpdateBaseSerializeMethodMixin


class MusicsSerializer(UpdateBaseSerializeMethodMixin, SpecifySerializeFieldModelSerializer):
    id = serializers.IntegerField()

    class Meta:
        model = BlogMusics
        fields = '__all__'


class PostMusicsSerializer(MusicsSerializer):

    def validate_music_cover(self, value):
        if whether_url(value):
            return value.strip()
        raise serializers.ValidationError("请输入合法的URL")

    def validate_music_url(self, value):
        if whether_url(value):
            return value.strip()
        raise serializers.ValidationError("请输入合法的URL")

    def validate_music_artist(self, value):
        return value.strip()

    def validate_music_name(self, value):
        return value.strip()


class UpdateMusicsSerializer(PostMusicsSerializer):

    def validate_id(self, value):
        if not BlogMusics.objects.filter(id=value).first():
            raise serializers.ValidationError("数据不存在")
        return value


class DeleteMusicsSerializer(UpdateMusicsSerializer):
    pass
